
<?php
if (!defined("ACCESS"))
    die("Access denied");

$id = $_REQUEST["id"];
$card_id = $_REQUEST["card_id"];

$panngan2_id = $_REQUEST["panngan2_id"];
$panngan3_id = $_REQUEST["panngan3_id"];

$ex1_id = $_REQUEST["ex1_id"];
$ex2_id = $_REQUEST["ex2_id"];
$ex3_id = $_REQUEST["ex3_id"];

$db = new myDBS();

if ($id) {
    $sql = "select * from tbl_promise_budget where id='$id'";
    $result1 = $db->runSQL($sql);
    $row1 = $db->fetch_object($result1);

    //$card_id = $row1->card_id;
}
$db->selectDB("hr_udoncity");
//
//$sql = "select * from person where id = '$card_id'";
//
//$result = $db->runSQL($sql);
//$row = $db->fetch_object($result);
?>

<!--<script src="js/jquery-1.7.1.min.js"></script>-->
<!--<script src="js/jquery-ui-1.8.20.custom.min.js"></script>-->


<script type="text/javascript">
    $(document).ready(function() {

        $("#e1").select2({
            placeholder: "เลือกผู้ขอยืม..."
            , allowClear: true
            , maximumSelectionSize: 2
                    //, tags:["red", "green", "blue"]
        });

//        $.post('dropdown.php', {tablename: 'expenses_lvl1', dd: 'ex1_id', idname: 'expenses1_id', fieldname: 'expenses1_name', by: '', ex1_id: '<?php echo $ex1_id; ?>', ex2_id: '<?php echo $ex2_id; ?>', ex3_id: '<?php echo $ex3_id; ?>'}, function(data) {
//            $("#expenses1").html(data);
//        });

        $.post('dropdown1.php', {tablename: 'expenses_lvl2', dd: 'ex2_id', idname: 'expenses2_id', fieldname: 'expenses2_name', by: '', ex2_id: '<?php echo $ex2_id; ?>', ex3_id: '<?php echo $ex3_id; ?>'}, function(data) {
            $("#expenses2").html(data);
        });

//        $("#panngan3_id").chained("#panngan2_id");

        $("#promise_date").datepicker({
            dateFormat: "yy-mm-dd"
//            ,showButtonPanel: true
//            ,defaultDate: new Date()
        });
        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                event.preventDefault();
                //alert("success");

                $.ajax({
                    type: "POST",
                    url: "promise_budget_process.php",
                    data: $('#frm_main').serialize(),
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {

//                        $("#dispStatus").html(data);
                        window.location.href = "index.php?pages=v_promise_budget";

                    }
                });
            }
        });


        //$('#panngan2_id').trigger('change');

        $("#panngan2_id").change(function() {
            var panngan2 = $(this).val();
            var panngan3 = "<?php echo $panngan3_id;  ?>";

//            alert(panngan2);

            if (panngan2 != "") {
                //alert(panngan2);
                $.post('dd_panngan.php', {panngan2: panngan2, panngan3: panngan3}, function(data) {
                    $("#dd_panngan3").html(data);

                });
            }

        });


        $.fn.myCalc = function() {
            var advances = $('#advances').val();
            var allowances = $('#allowances').val();
            var rest_rate = $('#rest_rate').val();
            var vehicle_rate = $('#vehicle_rate').val();
            var other = $('#other').val();
            var total = 0.00;

            advances = parseFloat(advances);
            allowances = parseFloat(allowances);
            rest_rate = parseFloat(rest_rate);
            vehicle_rate = parseFloat(vehicle_rate);
            other = parseFloat(other);

            if (isNaN(advances)) {
                advances = 0.00;
            }
            if (isNaN(allowances)) {
                allowances = 0.00;
            }
            if (isNaN(rest_rate)) {
                rest_rate = 0.00;
            }
            if (isNaN(vehicle_rate)) {
                vehicle_rate = 0.00;
            }
            if (isNaN(other)) {
                other = 0.00;
            }

            total = advances + allowances + rest_rate + vehicle_rate + other;

            $("#total").val(total);
            $("#total1").val(total);

//            var budget = $('#budget').val();
//            //budget = parseFloat(42341.13);
//            budget = parseFloat(budget);
//
//            //alert(budget);
//            var remain = budget - total;
//            //alert(total);
//            $("#remain").val(remain);
        }

        $('#advances').number(true, 2);
        $('#allowances').number(true, 2);
        $('#rest_rate').number(true, 2);
        $('#vehicle_rate').number(true, 2);
        $('#other').number(true, 2);
        $('#total').number(true, 2);
        $('#total1').number(true, 2);
//        $('#remain').number(true, 2);
//        $('#budget').number(true, 2);

        $("#advances").keyup(function() {
            $.fn.myCalc();
        });

        $('#allowances').keyup(function() {
            $.fn.myCalc();
        });

        $('#rest_rate').keyup(function() {
            $.fn.myCalc();
        });

        $('#vehicle_rate').keyup(function() {
            $.fn.myCalc();
        });
        $('#other').keyup(function() {
            $.fn.myCalc();
        });

//        $("#save_data").click(function() {
//            event.preventDefault();
//            alert("save_data");
//        });

    });


    function list_person(id) {
        $('<div>').dialog2({
            autoOpen: true,
            title: "เลือกรายชื่อ",
            content: "dlg_list_person.php?id=" + id,
            id: "modal-list-person"
        });
    }

    function get_budget() {
        //alert("afsd");
//        var panngan2_id = $("#panngan2_id").val();
//        var panngan3_id = $("#panngan3_id").val();
//
//        //alert (panngan3_id );
//        $.post('get_budget.php', {panngan2_id: panngan2_id, panngan3_id: panngan3_id}, function(data) {
//            $("#budget").val(data);
//        });

    }
</script>

<div id="dispStatus"></div>
<?php
//$panngan2_id
//if ($panngan2_id && $panngan3_id)
//echo get_budget('0101', '010102', '', '', '', '');
//echo "<br>";
//echo get_budget_used('0101', '010102', '03', '27', '8', '');
?>
<form class="form-horizontal" id="frm_main" action="#" method="post" novalidate=""> 
    <input type="hidden" id="id" name="id" value="<?php echo $id; ?>">
    <input type="hidden" id="task" name="task" value="<?php echo ($id) ? "edit" : "new"; ?>">
    <div class="row-fluid">
        <div class="span12">
            <div class="panel panel-info">
                <div class="panel-heading">สัญญายิมเงินงบประมาณ</div>
                <div class="panel-body">
                    <div class="control-group">
                        <label class="control-label" for="ปีงบประมาณ">ปีงบประมาณ</label>
                        <div class="controls">
                            <select class="span3" id="budget_year" name="budget_year" >
                                <?php
                                $this_y = date("Y");

                                if ($this_y < 2556)
                                    $this_y += 543;

                                if (!$budget_year)
                                    $budget_year = $this_y;

                                for ($i = 2556; $i <= $this_y + 1; $i++) {
                                    if ($i == $budget_year) {
                                        $selected = "selected";
                                    } else {
                                        $selected = "";
                                    }
                                    echo "<option value='$i' $selected>" . $i . "</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="วันที่ทำสัญญา">วันที่ทำสัญญา</label>
                        <div class="controls">
                            <input type="text" id="promise_date" name="promise_date" value="<?php echo ($id) ? $row1->promise_date : date('Y-m-d'); ?>" required>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="fullname">ชื่อ นามสกุล</label>
                        <div class="controls">
                            <select  id="e1" name="e1[]" multiple style="width: 250px;">
                                <!--<option value="1" selected>1111</option>-->
                                <?php
                                $sql = "select * from person ";

                                $result = $db->runSQL($sql);
                                while ($row2 = mysql_fetch_object($result)) {
                                    ?>
                                    <option value="<?php echo $row2->id; ?>" <?php echo ($row1->card_id1 == $row2->id || $row1->card_id2 == $row2->id || $row1->card_id3 == $row2->id) ? " selected" : ""; ?>><?php echo $row2->fname . "  " . $row2->lname; ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <!--                    <div class="control-group">
                                            <label class="control-label" for="fullname">ชื่อ นามสกุล</label>
                    
                                            <div class="controls">
                    
                                            </div>
                                        </div>
                    
                                        <div class="control-group">
                                            <label class="control-label" for="ตำแหน่ง">ตำแหน่ง</label>
                                            <div class="controls">
                                                <input type="text" value="<?php echo $row->p1; ?>" disabled>
                                            </div>
                                        </div>
                    
                                        <div class="control-group">
                                            <label class="control-label" for="สังกัด">สังกัด</label>
                                            <div class="controls">
                                                <input type="text"  value="<?php //echo $row->p1;                                              ?>" disabled>
                                            </div>
                                        </div>-->

                    <div class="control-group">
                        <label class="control-label" for="เหตุผลในการยืมเงิน">เหตุผลในการยืมเงิน</label>
                        <div class="controls">
                            <textarea rows="5" class="span5" id="reason" name="reason"><?php echo $row1->reason; ?></textarea>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="รายละเอียดเพิ่มเติม">รายละเอียดเพิ่มเติม</label>
                        <div class="controls">
                            <textarea rows="5" class="span5" id="details" name="details" required=""><?php echo $row1->details; ?></textarea>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span7">

            <div class="panel panel-info">
                <div class="panel-heading">มีความประสงค์ขอยืมเงินจาก</div>
                <div class="panel-body">    
                    <div class="control-group">
                        <label class="control-label" for="แผนงาน">แผนงาน</label>
                        <div class="controls">
                            <select id="panngan2_id" name="panngan2_id" >
                                <option value="">เลือก...</option>
                                <!--<option value="0101" >แผนงานบริหารงานทั่วไป</option>-->
                                <?php
//                                $sql = "SELECT  panngan2_id , panngan2_name "
//                                        . " FROM v_panngan2 "
//                                        . " WHERE lvl_id LIKE '" . $_SESSION["lvl1_id"] . "%'";
//
//                                echo "<option>$sql</option>";
//                                $result = $db->runSQL($sql);
//                                while ($r = $db->fetch_object($result)) {
////                                    if ($rows->panngan2_id == $panngan2) {
////                                        $selected = " selected";
////                                    } else {
////                                        $selected = "";
////                                    }
//                                    echo "<option value='" . $r->panngan2_id . "' $selected>$r->panngan2_name</option>";
//                                }
                                $db->selectDB("hr_udoncity");

                                $sql = "SELECT DISTINCT panngan2_id 
                                            FROM position 
                                            WHERE lvl_id like '" . $_SESSION["lvl1_id"] . "%'
                                            AND panngan2_id IS NOT NULL"
                                ;
                                $result = $db->runSQL($sql);

                                while ($rows = $db->fetch_object($result)) {
                                    $db->selectDB("account");
                                    $sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" . $rows->panngan2_id . "'";
                                    $result1 = $db->runSQL($sql);
                                    while ($rows1 = $db->fetch_object($result1)) {
                                        if ($rows1->panngan2_id == $panngan2) {
                                            $selected = " selected";
                                        } else {
                                            $selected = "";
                                        }
                                        echo "<option value='" . $rows1->panngan2_id . "' $selected>$rows1->panngan2_name</option>";
                                    }
                                }
                                ?>

                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="งาน">งาน</label>
                        <div class="controls" id="dd_panngan3">

                            <select  id="panngan3_id" name="panngan3_id">
                                <option value="">เลือก</option>

                            </select>

                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="expenses2_name">หมวด / ลักษณะ</label>
                        <div id="expenses2" class="controls">
                            <select id="ex2_id" name="ex2_id" required>
                                <option value="">เลือก...</option>
                            </select>
                        </div>

                    </div>


                    <div class="control-group">
                        <label class="control-label" for="expenses3_name">ประเภท</label>
                        <div id="expenses3" class="controls">
                            <select id="ex3_id" name="ex3_id" >
                                <option value="">เลือก...</option>

                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="โครงการ">โครงการ</label>
                        <div class="controls">
                            <select id="project" name="project" >
                                <option value="">เลือก...</option>
                                <option value="010102" >ไม่ระบุโครงการ</option>
                                <option value="010101">อื่นๆ</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>
        </div>
        <div class="span5">
            <div class="panel panel-info">
                <div class="panel-heading">งบประมาณ</div>
                <div class="panel-body"> 
                    <div class="control-group">
                        <label class="control-label" for="ค่าเบี้ยเลี้ยง">ค่าเบี้ยเลี้ยง</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="allowances" name="allowances" value="<?php echo ($row1->allowances) ? $row1->allowances : ""; ?>" placeholder="0.00">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="ค่าที่พัก">ค่าที่พัก</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="rest_rate" name="rest_rate" value="<?php echo ($row1->rest_rate) ? $row1->rest_rate : ""; ?>" placeholder="0.00">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="ค่าพาหนะ">ค่าพาหนะ</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="vehicle_rate" name="vehicle_rate" value="<?php echo ($row1->vehicle_rate) ? $row1->vehicle_rate : ""; ?>" placeholder="0.00">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="อื่นๆ">อื่นๆ</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="other" name="other" value="<?php echo ($row1->other) ? $row1->other : ""; ?>" placeholder="0.00">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="รวม">รวม</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="total" name="total" value="<?php echo ($row1->total) ? $row1->total : "0"; ?>" readonly="">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>       

    <div class="control-group">
        <div class="controls">
            <button class="btn btn-info" type="submit" >บันทึกข้อมูล</button>&nbsp;&nbsp;&nbsp;<a class="btn" href="index.php?pages=v_promise_budget">ยกเลิก</a>
        </div>  
    </div>
    <div class="row-fluid hide">
        <div class="panel panel-info">
            <div class="panel-heading">เพื่อเป็นค่าใช้จ่ายในการ</div>
            <div class="panel-body"> 
                <div class="span6">
                    <div class="control-group hidden">
                        <label class="control-label" for="ทดรองจ่ายในการดำเนินการ">ทดรองจ่ายในการดำเนินการ</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="advances" name="advances" value="<?php echo ($id) ? $row1->advances : ""; ?>" placeholder="0.00">
                        </div>
                    </div>
                </div>
                <div class="span6">
                    <!--                    <div class="control-group">
                                            <label class="control-label" for="ค่าเบี้ยเลี้ยง">ค่าเบี้ยเลี้ยง</label>
                                            <div class="controls">
                                                <input class="text-right" type="text" id="allowances" name="allowances" value="<?php echo ($row1->allowances) ? $row1->allowances : ""; ?>" placeholder="0.00"
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label" for="ค่าที่พัก">ค่าที่พัก</label>
                                            <div class="controls">
                                                <input class="text-right" type="text" id="rest_rate" name="rest_rate" value="<?php echo ($row1->rest_rate) ? $row1->rest_rate : ""; ?>" placeholder="0.00"
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label" for="ค่าพาหนะ">ค่าพาหนะ</label>
                                            <div class="controls">
                                                <input class="text-right" type="text" id="vehicle_rate" name="vehicle_rate" value="<?php echo ($row1->vehicle_rate) ? $row1->vehicle_rate : ""; ?>" placeholder="0.00"
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label" for="อื่นๆ">อื่นๆ</label>
                                            <div class="controls">
                                                <input class="text-right" type="text" id="other" name="other" value="<?php echo ($row1->other) ? $row1->other : ""; ?>" placeholder="0.00"
                                            </div>
                                        </div>
                                        <div class="control-group">
                                            <label class="control-label" for="รวม">รวม</label>
                                            <div class="controls">
                                                <input class="text-right" type="text" id="total" name="total" value="<?php echo ($row1->total) ? $row1->total : "0"; ?>" readonly="">
                                            </div>
                                        </div>-->
                </div>


            </div>
        </div>

    </div>

</form>


<?php ?>
